package com.sctvcloud.qingchuan.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.beans.NewsClickCountBean;
import com.sctvcloud.qingchuan.beans.NewsIdBean;
import com.sctvcloud.qingchuan.beans.NewsItem;
import com.sctvcloud.qingchuan.beans.NewsListClickRequestBean;
import com.sctvcloud.qingchuan.beans.NewsListClickVo;
import com.sctvcloud.qingchuan.beans.ServiceDataBean;
import com.sctvcloud.qingchuan.beans.TopicSubBean;
import com.sctvcloud.qingchuan.http.AbsListNetCallback;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.activities.SubTopicActivity;
import com.sctvcloud.qingchuan.ui.adapter.ServiceRecentlyUsedServiceAdapter;
import com.sctvcloud.qingchuan.ui.adapter.SubTopicNewsListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 新版 专题详情下方的新闻列表
 */
public class SubTopicNewsListFragment extends ColumnBaseFragment {

    @BindView(R.id.news_list)
    protected RecyclerView recyclerView;
    @BindView(R.id.title)
    TextView mTitleTextView;
    @BindView(R.id.rl)
    View mTitleLayout;

    private TopicSubBean topicData;
    private SubTopicNewsListAdapter adapter;
    private List<NewsItem> newsItems;
    private boolean isUseTitle = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sub_newslist, container, false);
        ButterKnife.bind(this, view);
        initRecentlyView();
        return view;
    }

    private void initRecentlyView() {
        LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(manager);
        adapter = new SubTopicNewsListAdapter(getActivity(), null);
        recyclerView.setAdapter(adapter);
//        adapter.setData(serviceList);
        getNewsData();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    /**
     * 赋值子专题相关数据
     *
     * @param topicData
     */
    public void setTopicData(TopicSubBean topicData) {
        this.topicData = topicData;
    }

    public void setViewTitle(boolean use) {
        isUseTitle = use;
    }

    /**
     * 请求专题新闻列表
     */
    private void getNewsData() {
        mTitleLayout.setVisibility(isUseTitle ? View.VISIBLE : View.GONE);

        if (null != topicData) {
            mTitleTextView.setText(topicData.getSubTopicTitle());

            NetUtils.getNetAdapter().getNewsArray(getOwnerName(), topicData.getSubTopicUrl(), new AbsListNetCallback<NewsItem>(NewsItem.class) {
                @Override
                public void onSuc(List<NewsItem> datas) {
                    if (ListUtils.isListValued(datas)) {
                        requestNewsClickCount(datas);
                    }
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
                }

                @Override
                public void onEnd() {
                }
            });
        }
    }

    /**
     * 获取新闻列表点击量
     *
     * @param list
     */
    private void requestNewsClickCount(List<NewsItem> list) {
        if (ListUtils.isListValued(list)) {
            newsItems = new ArrayList<>();
            newsItems.addAll(list);
            List<NewsIdBean> newsIdList = new ArrayList<>();
            NewsIdBean newsBean;
            for (int i = 0; i < list.size(); i++) {
                String type = "10";//默认不返回点击量
                if (list.get(i) instanceof NewsItem) {
                    NewsItem item = (NewsItem) list.get(i);
                    newsBean = new NewsIdBean();
                    newsBean.setId(item.getUrlNewsId());
                    switch (item.getNewsType()) {
                        case Constances.NEWS_TYPE_GRAPHIC:
                        case Constances.NEWS_TYPE_AUDIO:
                        case Constances.NEWS_TYPE_VIDEO:
                            //新闻详情
                            type = "0";
                            break;
                        case Constances.NEWS_TYPE_H5:
                            //新闻详情
                            type = "0";
                            newsBean.setId(item.getNewsId());
                            break;
                        case Constances.NEWS_TYPE_LIVE:
                            //直播详情
                            type = "1";
                            break;
                        default:
                            //其他详情
                            newsBean.setId(item.getNewsId());
                            type = "10";
                            break;
                    }
                    newsBean.setType(type);
                    newsIdList.add(newsBean);
                } else {
                    newsBean = new NewsIdBean();
                    newsBean.setId("1");
                    newsBean.setType(type);
                    newsIdList.add(newsBean);
                }
            }
            if (ListUtils.isListValued(newsIdList)) {
                NewsListClickRequestBean requestBean = new NewsListClickRequestBean();
                requestBean.setNewsIds(newsIdList);
                Gson gson = new Gson();
                String newsId = gson.toJson(requestBean);
                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), newsId);
                NetUtils.getNetAdapter().getViewsList(getOwnerName(), body, new AbsNetCallBack<NewsListClickVo>(NewsListClickVo.class) {
                    @Override
                    public void onSuc(NewsListClickVo newsListClickVo) {
                        if (null != newsListClickVo && ListUtils.isListValued(newsListClickVo.getResult())) {
                            List<NewsClickCountBean> countList = newsListClickVo.getResult();
                            for (int i = 0; i < countList.size(); i++) {
                                if (newsItems.get(i) instanceof NewsItem) {
                                    NewsItem item = (NewsItem) newsItems.get(i);
                                    if (item.getNewsType() == Constances.NEWS_TYPE_H5) {
                                        if (countList.get(i).getId().equalsIgnoreCase(item.getNewsId())) {
                                            ((NewsItem) newsItems.get(i)).setReadCount(countList.get(i).getViews());
                                        }
                                    } else if (countList.get(i).getId().equalsIgnoreCase(item.getUrlNewsId())) {
                                        ((NewsItem) newsItems.get(i)).setReadCount(countList.get(i).getViews());
                                    }
                                }
                            }
                        }

                    }

                    @Override
                    public void onEnd() {
                        super.onEnd();
                        adapter.setDataList(newsItems);
                    }

                    @Override
                    public void onError(Throwable ex, String errorMsg) {
                        super.onError(ex, errorMsg);
                    }
                });
            }
        }
    }


    @OnClick({R.id.more})
    void onViewClick(View view) {
        if (topicData != null) {
            SubTopicActivity.start(topicData);
        }
    }
}
